package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientSupplierService;
import com.mdd.admin.validate.ClientSupplierCreateValidate;
import com.mdd.admin.validate.ClientSupplierUpdateValidate;
import com.mdd.admin.validate.ClientSupplierSearchValidate;
import com.mdd.admin.vo.ClientSupplierListedVo;
import com.mdd.admin.vo.ClientSupplierDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientSupplier;
import com.mdd.common.mapper.ClientSupplierMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 供应商实现类
 * @author LikeAdmin
 */
@Service
public class ClientSupplierServiceImpl implements IClientSupplierService {
        
    @Resource
    ClientSupplierMapper clientSupplierMapper;

    /**
     * 供应商列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientSupplierListedVo>
     */
    @Override
    public PageResult<ClientSupplierListedVo> list(PageValidate pageValidate, ClientSupplierSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientSupplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientSupplierMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:firmName@firm_name:str",
            "=:type:str",
            "between:dateCheck@date_check:str",
            "=:coopStatus@coop_status:str",
            "=:paymentTermid@payment_termId:int",
        });

        IPage<ClientSupplier> iPage = clientSupplierMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientSupplierListedVo> list = new LinkedList<>();
        for(ClientSupplier item : iPage.getRecords()) {
            ClientSupplierListedVo vo = new ClientSupplierListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 供应商详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientSupplier
     */
    @Override
    public ClientSupplierDetailVo detail(Integer id) {
        ClientSupplier model = clientSupplierMapper.selectOne(
                new QueryWrapper<ClientSupplier>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientSupplierDetailVo vo = new ClientSupplierDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 供应商新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientSupplierCreateValidate createValidate) {
        ClientSupplier model = new ClientSupplier();
        model.setFirmName(createValidate.getFirmName());
        model.setType(createValidate.getType());
        model.setDateCheck(createValidate.getDateCheck());
        model.setDart(createValidate.getDart());
        model.setTown(createValidate.getTown());
        model.setZone(createValidate.getZone());
        model.setProvinceid(createValidate.getProvinceid());
        model.setFullAddress(createValidate.getFullAddress());
        model.setLinkman(createValidate.getLinkman());
        model.setPhone(createValidate.getPhone());
        model.setServingProductid(createValidate.getServingProductid());
        model.setServiceTariffing(createValidate.getServiceTariffing());
        model.setCoopStatus(createValidate.getCoopStatus());
        model.setDueBank(createValidate.getDueBank());
        model.setRemark(createValidate.getRemark());
        model.setReceivingBankAccount(createValidate.getReceivingBankAccount());
        model.setPaymentTermid(createValidate.getPaymentTermid());
        model.setPaymentCodeUpload(createValidate.getPaymentCodeUpload());
        model.setUserId(createValidate.getUserId());
        clientSupplierMapper.insert(model);
    }

    /**
     * 供应商编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientSupplierUpdateValidate updateValidate) {
        ClientSupplier model = clientSupplierMapper.selectOne(
                new QueryWrapper<ClientSupplier>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setFirmName(updateValidate.getFirmName());
        model.setType(updateValidate.getType());
        model.setDateCheck(updateValidate.getDateCheck());
        model.setDart(updateValidate.getDart());
        model.setTown(updateValidate.getTown());
        model.setZone(updateValidate.getZone());
        model.setProvinceid(updateValidate.getProvinceid());
        model.setFullAddress(updateValidate.getFullAddress());
        model.setLinkman(updateValidate.getLinkman());
        model.setPhone(updateValidate.getPhone());
        model.setServingProductid(updateValidate.getServingProductid());
        model.setServiceTariffing(updateValidate.getServiceTariffing());
        model.setCoopStatus(updateValidate.getCoopStatus());
        model.setDueBank(updateValidate.getDueBank());
        model.setRemark(updateValidate.getRemark());
        model.setReceivingBankAccount(updateValidate.getReceivingBankAccount());
        model.setPaymentTermid(updateValidate.getPaymentTermid());
        model.setPaymentCodeUpload(updateValidate.getPaymentCodeUpload());
        model.setUserId(updateValidate.getUserId());
        clientSupplierMapper.updateById(model);
    }

    /**
     * 供应商删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientSupplier model = clientSupplierMapper.selectOne(
                new QueryWrapper<ClientSupplier>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientSupplierMapper.updateById(model);
    }

}
